require "Common/define"

---@class CtrlManager
CtrlManager = {};
local this = CtrlManager;
local ctrlList = {};	--控制器列表--

--- 需要清理的LuaManager
local ClearLuaManager = {
	ActiveManager = "ActiveManager",
	ArenaManager = "ArenaManager",
	BattlePlaybackManager = "BattlePlaybackManager",
	ConnectManager = "ConnectManager",
	CostManager = "CostManager",
	EcoBuffManager = "EcoBuffManager",
	EmailManager = "EmailManager",
	--EquipGrade = "EquipGrade",
	EquipManager = "EquipManager",
	ExpeditionMgr = "ExpeditionMgr",
	FetterModel = "FetterModel",
	FlashGiftManager = "FlashGiftManager",
	FormationManager = "FormationManager",
	FriendBattleManager = "FriendBattleManager",
	FriendManager = "FriendManager",
	FubenModel = "FubenModel",
	FuncOpenManager = "FuncOpenManager",
	GuideMgr = "GuideMgr",
	HeroLinkManager = "HeroLinkManager",
	ItemManager = "ItemManager",
	MapManager = "MapManager",
	MythicDungeonsManager = "MythicDungeonsManager",
	NoticeManager = "NoticeManager",
	PetManager = "PetManager",
	ShopManager = "ShopManager",
	SkillManager = "SkillManager",
	TacticsManager = "TacticsManager",
	TowerManager = "TowerManager",
	TrialsManager = "TrialsManager",
	UIShowManager = "UIShowManager",
	VipManager = "VipManager",
	HeroManager = "HeroManager",
	TaskManager = "TaskManager",
	ChatManager = "ChatManager",
	RankBattleManager = "RankBattleManager",
	ItemFlyMgr = "ItemFlyMgr",
	CityPlayerMgr = "CityPlayerMgr",
	GuildManager = "GuildManager",
	GuildBattleManager = "GuildBattleManager",
	DarkStreetMgr = "DarkStreetMgr",
	StrangeFragmentManager = "StrangeFragmentManager",
	FrameManager = "FrameManager",
	RealManWarManager = "RealManWarManager",
	BrokerManager = "BrokerManager",
	GuildBattlePlusManager = "GuildBattlePlusManager",
	SkinManager = "SkinManager",
	PasteFaceNoticeManager = "PasteFaceNoticeManager",
	DungeonRandomEventManager = "DungeonRandomEventManager",
	UnlockFuncManager = "UnlockFuncManager",
	NewPlayerActiveManager = "NewPlayerActiveManager",
	SoloChaosTestManager = "SoloChaosTestManager",
	StoryHardManager = "StoryHardManager",
	HeroSoulManager = "HeroSoulManager",
	HeroPotentialMgr = "HeroPotentialMgr",
	LongJourneyHellManager = "LongJourneyHellManager", -- 旋云远征深渊
	DivineInspirationManager = "DivineInspirationManager",	--启迪圣书
	RuneManager = "RuneManager", --符文系统
	HeroManorManager = "HeroManorManager",  --家园系统
}

function CtrlManager.Init()
	return this;
end

--添加控制器--
function CtrlManager.AddCtrl(ctrlName, ctrlObj)
	ctrlName = getUIName(ctrlName);
	ctrlList[ctrlName] = ctrlObj;
end

--获取控制器--
function CtrlManager.GetCtrl(ctrlName , load)
	local name  = getUIName(ctrlName);
	if ctrlList[name] == nil then
		if load == nil then 
			this.AddScript(ctrlName);
		end
	end
	return ctrlList[name];
end

--移除控制器--
function CtrlManager.RemoveCtrl(ctrlName)
	local packName = string.gsub(ctrlName, "/", ".")
	ctrlName = getUIName(ctrlName)
	if ctrlList[ctrlName] then
		ctrlList[ctrlName] = nil
	end

	packName = "View."..packName
	if package.loaded[packName] then
		package.loaded[packName] = nil
		_G[packName] = nil
	end
end

--关闭控制器--
function CtrlManager.Close()
	logWarn('CtrlManager.Close---->>>');
end

--require控制器
function CtrlManager.AddScript(ctrlName)
	if ctrlList[ctrlName] then 
		return
	end
	local names = string_split(ctrlName,"/")
	local name = names[#names]
	local luaName = PanelNames[name]
	local ctrl = require ("View/"..luaName);
	this.AddCtrl(ctrlName,ctrl);
end


function CtrlManager.ClearLuaTable()
	for name, _ in pairs(ClearLuaManager) do
		local packName = "Logic."..name
		if package.preload[packName] then
			package.preload[packName] = nil
		end
		if package.loaded[packName] then
			package.loaded[packName] = nil
		end
		if _G[name] then
			_G[name] = nil
		end
		if logicMgr and logicMgr[name] then
			if logicMgr[name].Destroy then
				logicMgr[name].Destroy()
			end
			if logicMgr[name].OnDestroy then
				logicMgr[name].OnDestroy()
			end
			logicMgr[name] = nil
		end
	end
end

return CtrlManager